package com.dk.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 注册拦截器
 */
@Configuration
public class WebConfigurer extends WebMvcConfigurationSupport {

    @Resource
    private LoginInterceptor loginHandlerInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration ir = registry.addInterceptor(loginHandlerInterceptor);
        // 拦截路径
        ir.addPathPatterns("/users/*");
        ir.addPathPatterns("/menu/*");
        ir.addPathPatterns("/part/*");

        // 不拦截路径
        List<String> irs = new ArrayList<String>();
        irs.add("/users/login");
        irs.add("/users/readexcl");
        irs.add("/users/download");
//        irs.add("/menu/getmenu");
//        irs.add("/wechat/*");
//        irs.add("/oauth/*");
        ir.excludePathPatterns(irs);
    }
    /**
     * 发现如果继承了WebMvcConfigurationSupport，则在yml中配置的相关内容会失效。 需要重新指定静态资源
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html", "doc.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }
}
